import Vue from 'vue'
import {Login} from "../service/loginApi";
import {getMenuList} from "../service/menu";
import {
  addUser,
  deleteUserById,
  editUserInfo,
  getUserById,
  getUsersList, saveRoleInfo,
  updateUserState
} from "../service/usersApi";
import {
  addRoles, allotRights,
  deleteRolesById,
  getRightsList, getRightsTree,
  getRolesById,
  getRolesList,
  removeRightById,
  updateRoles
} from "../service/rightsApi";
import {addCate, getCateById, getCateList, getParentCateList, removeCateById, updateCateInfo} from "../service/cateApi";
import {
  addParams, editAddParams,
  getCateParamsList,
  getParamsById,
  getParamsList,
  removeParamsById,
  updateParamsById
} from "../service/paramsApi";
import {addGoods, deleteGoodsById, getGoodsById, getGoodsList} from "../service/goodsApi";
import {getOrdersById, getOrdersList, getWuliuById} from "../service/orderApi";
import {getReport} from "../service/reportApi";

export default {

  async Login_({commit}, payload) {
    await Login(payload).then(res => {
      if (res.meta.status === 200) {
        window.sessionStorage.setItem('token', res.data.token)
      } else {
      }
      commit("LoginMutation", res)
    }).catch((e) => {
      console.log(e);
    })
  },

  async getMenuList({commit}, payload) {
    await getMenuList().then(res => {
      if (res.meta.status === 200) {
        commit("MenuListMutation", res.data)
      } else {
        Vue.prototype.$message.error("请求失败！")
      }

    }).catch((e) => {
      console.log(e);
    })
  },

  async getUsersList_({commit}, payload) {
    try {
      const res = await getUsersList(payload)
      if (res.meta.status === 200) {
        commit("UserListMutation", res.data)
      } else {
        Vue.prototype.$message.error("请求失败！")
      }
    } catch (e) {
      console.log(e);
    }
  },

  async updateUserState_({commit}, payload) {
    try {
      const res = await updateUserState(payload)
      if (res.meta.status === 200) {
        commit("UserMutation", res)
      } else {
        Vue.prototype.$message.error("更新失败！")
      }
    } catch (e) {
      console.log(e);
    }
  },

  async addUser_({commit}, payload) {
    try {
      const res = await addUser(payload)
      if (res.meta.status === 201) {
        Vue.prototype.$message.success("添加用户成功")
        commit("UserMutation", res)
      } else {
        Vue.prototype.$message.error("添加失败！")
      }
    } catch (e) {
      console.log(e);
    }
  },

  async getUserById_({commit}, payload) {
    try {
      const res = await getUserById(payload)
      commit("UserMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async editUserInfo_({commit}, payload) {
    try {
      const res = await editUserInfo(payload)
      commit("UserMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async deleteUserById_({commit}, payload) {
    try {
      const res = await deleteUserById(payload)
      commit("UserMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getRightsList_({commit}, payload) {
    try {
      const res = await getRightsList(payload)
      commit("RightsListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getRolesList_({commit}, payload) {
    try {
      const res = await getRolesList(payload)
      commit("RolesListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async addRoles_({commit}, payload) {
    try {
      const res = await addRoles(payload)
      commit("RolesMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async updateRoles_({commit}, payload) {
    try {
      const res = await updateRoles(payload)
      commit("RolesMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getRolesById_({commit}, payload) {
    try {
      const res = await getRolesById(payload)
      commit("RolesMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async deleteRolesById_({commit}, payload) {
    try {
      const res = await deleteRolesById(payload)
      commit("RolesMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async removeRightById_({commit}, payload) {
    try {
      const res = await removeRightById(payload)
      commit("RolesMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getRightsTree_({commit}, payload) {
    try {
      const res = await getRightsTree(payload)
      commit("RightsListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async allotRights_({commit}, payload) {
    try {
      let res = await allotRights(payload)
      if (res.meta.status === 200) {
        commit("AllotRightsMutation", res)
      }
    } catch (e) {
      console.log(e);
    }
  },

  async saveRoleInfo_({commit}, payload) {
    try {
      let res = await saveRoleInfo(payload)
      commit("SaveRoleInfo", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getCateList_({commit}, payload) {
    try {
      let res = await getCateList(payload)
      commit("CateListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getParentCateList_({commit}, payload) {
    try {
      let res = await getParentCateList(payload)
      commit("ParentCateListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async addCate_({commit}, payload) {
    try {
      let res = await addCate(payload)
      commit("CateMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async getCateById_({commit}, payload) {
    try {
      let res = await getCateById(payload)
      commit("CateMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async updateCateInfo_({commit}, payload) {
    try {
      let res = await updateCateInfo(payload)
      commit("CateMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async removeCateById_({commit}, payload) {
    try {
      let res = await removeCateById(payload)
      commit("CateMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getCateParamsList_({commit}, payload) {
    try {
      let res = await getCateParamsList(payload)
      commit("ParamsCateListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getParamsList_({commit}, payload) {
    try {
      let res = await getParamsList(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async addParams_({commit}, payload) {
    try {
      let res = await addParams(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getParamsById_({commit}, payload) {
    try {
      let res = await getParamsById(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async updateParamsById_({commit}, payload) {
    try {
      let res = await updateParamsById(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async removeParamsById_({commit}, payload) {
    try {
      let res = await removeParamsById(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async editAddParams_({commit}, payload) {
    try {
      let res = await editAddParams(payload)
      commit("ParamsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async getGoodsList_({commit}, payload) {
    try {
      let res = await getGoodsList(payload)
      commit("GoodsListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async deleteGoodsById_({commit}, payload) {
    try {
      let res = await deleteGoodsById(payload)
      commit("GoodsListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async addGoods_({commit}, payload) {
    try {
      let res = await addGoods(payload)
      commit("GoodsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
  async getGoodsById_({commit}, payload) {
    try {
      let res = await getGoodsById(payload)
      commit("GoodsMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getOrdersList_({commit}, payload) {
    try {
      let res = await getOrdersList(payload)
      commit("OrdersListMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getOrdersById_({commit}, payload) {
    try {
      let res = await getOrdersById(payload)
      commit("OrderMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getWuliuById_({commit}, payload) {
    try {
      let res = await getWuliuById(payload)
      commit("WLMutation", res)
    } catch (e) {
      console.log(e);
    }
  },

  async getReport_({commit}, payload) {
    try {
      let res = await getReport(payload)
      commit("ReportMutation", res)
    } catch (e) {
      console.log(e);
    }
  },
}
